import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:wanchong/api/know.dart';
import 'package:wanchong/bean/video_list_entity.dart';
import '../../path.dart';

class Videoview extends StatefulWidget {
  @override 
  _ZsviewState createState() => _ZsviewState();
}

class _ZsviewState extends State<Videoview> {
  List<VideoListData> _videoList = List();
  int pages = 1;
 GlobalKey<EasyRefreshState> _easyRefreshKey =
      new GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey =
      new GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey =
      new GlobalKey<RefreshFooterState>();
  @override
  void initState() {
    super.initState();
    if (_videoList.length == 0) {
      Myvedio(1).listen((VideoListEntity data) {
        if (data.code == 200) {
          setState(() {
            this._videoList = data.data;
            this.pages = 2;
          });
        } else {}
      });
    }
  }

  Widget build(BuildContext context) {
    return EasyRefresh(
      key: _easyRefreshKey,
      behavior: ScrollOverBehavior(),
      refreshHeader: BezierHourGlassHeader(
        key: _headerKey,
        color: Color(0xFFFFCC33),
        backgroundColor: Colors.white,
      ),
      refreshFooter: BezierBounceFooter(
        key: _footerKey,
        color: Color(0xFFFFCC33),
        backgroundColor: Colors.white,
      ),
      child: _listSp(),
      onRefresh: () async {
        await new Future.delayed(const Duration(seconds: 1), () {
          setState(() {
            Myvedio(1).listen((VideoListEntity data) {
              if (data.code == 200) {
                setState(() {
                  this._videoList.clear();
                  this._videoList = data.data;
                  this.pages = 2;
                });
              } else {}
            });
          });
        });
      },
      loadMore: () async {
        await new Future.delayed(const Duration(seconds: 1), () {
          setState(() {
            Myvedio(pages).listen((VideoListEntity data) {
              if (data.code == 200) {
                setState(() {
                  this._videoList.addAll(data.data);
                  this.pages++;
                });
              } else {}
            });
          });
        });
      },
    );
  }

  Widget _listSp() {
    List<Widget> tiles = [];
    Widget ContainerGl;
    for (var item in _videoList) {
      tiles.add(new GestureDetector(
        onTap: () {
          Navigator.of(context).pushNamed(VidPath);
        },
        child: Stack(
          children: <Widget>[
            Container(
                height: 231.0,
                decoration: BoxDecoration(
                    shape: BoxShape.rectangle,
                    borderRadius: BorderRadius.circular(6.0),
                    image: DecorationImage(
                      fit: BoxFit.cover,
                      image: NetworkImage(
                          'http://pic37.nipic.com/20140113/8800276_184927469000_2.png'),
                    ))),
            Positioned(
                bottom: 10.0,
                left: 10.0,
                child: new Container(
                  child: Text(
                    item.agree,
                    style: new TextStyle(fontSize: 12.0, color: Colors.white),
                  ),
                )),
            Positioned(
              child: Image.asset(
                'assets/user/play-icon.png',
                fit: BoxFit.contain,
              ),
              width: 20.0,
              height: 20.0,
              bottom: 10.0,
              right: 10.0,
            ),
          ],
        ),
      ));
    }
    ContainerGl = new GridView.count(
      //水平子Widget之间间距
      crossAxisSpacing: 10.0,
      //垂直子Widget之间间距
      mainAxisSpacing: 10.0,
      //GridView内边距
      padding: EdgeInsets.all(10.0),
      //一行的Widget数量
      crossAxisCount: 2,
      //子Widget宽高比例
      childAspectRatio: 0.75,
      children: tiles,
      physics: BouncingScrollPhysics(),
    );
    return ContainerGl;
  }
}
